package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void save(Role role, Long[] ids) {
        //检查参数合法性
        //假设业务很复杂
        roleMapper.insert(role);

        if (ids != null && ids.length>0){
            roleMapper.insertRelation(role.getId(),ids);
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }


    /*@Override
    public PageResultBak<Role> query(QueryObject qo) {
        int count = roleMapper.selectForCount(qo);
        if (count == 0) {
            return new PageResultBak<>(qo.getCurrentPage(), qo.getPageSize()
                    , Collections.emptyList(), count);
        }
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageResultBak<>(qo.getCurrentPage(), qo.getPageSize(), roles, count);
    }*/
    public PageInfo<Role> query(QueryObject qo) {
        //工具的分页插件
        //可以点startPage,再点下载源码,就能看见中文注释
        //业务层控制器要跟着修改
        //开始分页(需要传入当前页,传入每一页显示数量),下一句
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //sql不要写limit,插件会自动在sql语句后面加上limit语句
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo<>(roles);
    }

}
